package ca.uwo.android;

import ca.uwo.android.R;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	
	/**
	 * The name and order of the names that appear below the icons
	 * Read from the strings.xml file
	 */
	private String[] iconNames;
	
	/**
	 * The name and order of the icons that appear 
	 */
	private Integer[] moduleIcons;
	
	
	public ImageAdapter(Context c, String[] iconNames, Integer[] moduleIcons) {
		
//		Log.e("How often called?", "Image adapter constructor called.");
		
		//get inflater
		inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		Resources res = c.getResources();
		this.iconNames = iconNames;
		this.moduleIcons = moduleIcons;

	}
	
	@Override
	public int getCount() {
		return moduleIcons.length;
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout view;
		ImageView imageView;
		TextView textView;
		if (convertView == null) {  // if it's not recycled, initialize some attributes
			view = (LinearLayout)inflater.inflate(R.layout.grid_item, null);
		} 
		else {
			view = (LinearLayout) convertView;
		}        
		imageView = (ImageView)view.findViewById(R.id.moduleIcon);
		textView = (TextView)view.findViewById(R.id.moduleText);

		imageView.setImageResource(moduleIcons[position]);
		textView.setText(iconNames[position]);
		return view;	
	}


}
